<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    :maskClosable="false"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="学生信息">
          <a-input :value="studentInfo" disabled />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="宿舍房间" v-if="actionType !== 'leave'">
          <a-select
            placeholder="请选择宿舍房间"
            v-decorator="['roomId', { rules: [{ required: true, message: '请选择宿舍房间!' }] }]"
          >
            <a-select-option v-for="room in roomList" :key="room.id" :value="room.id">
              {{ room.buildingName }} - {{ room.roomCode }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="床位号" v-if="actionType !== 'leave'">
          <a-input-number
            placeholder="请输入床位号"
            style="width: 100%"
            :min="1"
            :max="4"
            v-decorator="['bedNo', { rules: [{ required: true, message: '请输入床位号!' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="变更原因">
          <a-textarea
            placeholder="请输入变更原因"
            :rows="4"
            v-decorator="['reason']"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import { getAction } from '@/api/manage'

  export default {
    name: 'DormitoryStudentRoomModal',
    data () {
      return {
        form: this.$form.createForm(this),
        title: '',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        url: {
          assignRoom: '/dormitory/student/assignRoom',
          changeRoom: '/dormitory/student/changeRoom',
          leaveRoom: '/dormitory/student/leaveRoom',
          listRooms: '/dormitory/room/listAll'
        },
        roomList: [],
        studentId: '',
        actionType: '', // assign, change, leave
      }
    },
    computed: {
      studentInfo () {
        if (!this.model || !this.model.studentNo) {
          return '';
        }
        return `${this.model.studentNo} - ${this.model.name}`;
      }
    },
    created () {
    },
    methods: {
      // 分配宿舍
      assignRoom (record) {
        this.actionType = 'assign';
        this.title = '分配宿舍';
        this.loadRooms();
        this.edit(record);
      },
      // 调换宿舍
      changeRoom (record) {
        this.actionType = 'change';
        this.title = '调换宿舍';
        this.loadRooms();
        this.edit(record);
      },
      // 退宿
      leaveRoom (record) {
        this.actionType = 'leave';
        this.title = '退宿';
        this.edit(record);
      },
      // 加载宿舍列表
      loadRooms () {
        getAction(this.url.listRooms).then((res) => {
          if (res.success) {
            this.roomList = res.result;
          }
        });
      },
      // 编辑
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.studentId = record.id;
        this.visible = true;
      },
      // 确定
      handleOk () {
        const that = this;
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpUrl = '';
            let method = 'post';
            
            if (this.actionType === 'assign') {
              httpUrl = this.url.assignRoom;
              values.studentId = this.studentId;
            } else if (this.actionType === 'change') {
              httpUrl = this.url.changeRoom;
              values.studentId = this.studentId;
              values.newRoomId = values.roomId;
              values.newBedNo = values.bedNo;
            } else if (this.actionType === 'leave') {
              httpUrl = this.url.leaveRoom;
              values = {
                studentId: this.studentId,
                reason: values.reason
              };
            }
            
            httpAction(httpUrl, values, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close();
      },
      close () {
        this.$emit('close');
        this.visible = false;
      }
    }
  }
</script>